package cn.appsys.service.impl;

import cn.appsys.dao.appCategorydDao.AppCategoryMapper;
import cn.appsys.dao.appInfoDao.AppInfoMapper;
import cn.appsys.dao.appVersionDao.AppVersionMapper;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.service.AppInfoService;
import cn.appsys.tools.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

@Service("AppInfoService")
public class AppInfoServiceImpl implements AppInfoService {

    private static final Logger LOOGER=Logger.getLogger(AppInfoServiceImpl.class);

    @Resource
    private AppInfoMapper appInfoMapper;

    @Resource
    private AppCategoryMapper appCategoryMapper;


    @Resource
    private AppVersionMapper appVersionMapper;

    @Override
    public boolean saveAppInfo(AppInfo appInfo) {
        int row=0;
        try {
            row=appInfoMapper.addAppInfo(appInfo);
            if(row==1){
                LOOGER.debug("appInfoMapper.addAppInfo 查询成功");
                return true;
            }else{
               LOOGER.debug("appInfoMapper.addAppInfo 查询失败");
                return false;
            }
        } catch (Exception e) {
            LOOGER.error("appInfoMapper.addAppInfo 查询异常");
            return false;
        }

    }

    @Override
    public AppInfo getByAPKName(String APKName) {
        AppInfo appInfo=null;
        try {
            appInfo=appInfoMapper.findByAPKName(APKName);
            if(null!=appInfo){
                LOOGER.debug("appInfoMapper.findByAPKName 查询失败");
                appInfo=new AppInfo();
            }else{
                LOOGER.debug("appInfoMapper.findByAPKName 查询成功");
            }
        } catch (Exception e) {
            LOOGER.error("appInfoMapper.findByAPKName 查询异常");
            appInfo=new AppInfo();
        }
        return appInfo;
    }

    @Override
    public List<AppInfo> getAllAppInfoByMsg(PageSupport ps, String swName, Integer status, Integer flatformId, Integer pid1, Integer pid2, Integer pid3) {
        List<AppInfo> appInfoList = new ArrayList<>();
        try {
            //更新总条数
            int count = appInfoMapper.getCountByMsg(swName,status,flatformId,pid1,pid2,pid3);
            ps.setTotalCount(count);

            int s = (ps.getCurrentPageNo()-1)*ps.getPageSize();
            int a = ps.getPageSize();

            appInfoList = appInfoMapper.findAllByMsg(s,a,swName,status,flatformId,pid1,pid2,pid3);
            if(null == appInfoList){
                LOOGER.debug("AppInfoServiceImpl.getAllAppInfoByMsg 查询为空》》》");
            }else{
                LOOGER.debug("AppInfoServiceImpl.getAllAppInfoByMsg 查询成功》》》");
                for (AppInfo appInfo:appInfoList) {
                    //平台
                    switch (appInfo.getFlatformId()){
                        case 1:
                            appInfo.setFlatformName("手机");
                            break;
                        case 2:
                            appInfo.setFlatformName("平板");
                            break;
                        case 3:
                            appInfo.setFlatformName("通用");
                            break;
                    }
                    //所属状态
                    switch (appInfo.getStatus()){
                        case 1:
                            appInfo.setStatusName("待审核");
                            break;
                        case 2:
                            appInfo.setStatusName("审核通过");
                            break;
                        case 3:
                            appInfo.setStatusName("审核不通过");
                            break;
                        case 4:
                            appInfo.setStatusName("已上架");
                            break;
                        case 5:
                            appInfo.setStatusName("已下架");
                            break;
                    }
                    //所属分类名
                    appInfo.setCategoryLevel1Name(appCategoryMapper.qryCategoryById(appInfo.getCategoryLevel1()).getCategoryName());
                    appInfo.setCategoryLevel2Name(appCategoryMapper.qryCategoryById(appInfo.getCategoryLevel2()).getCategoryName());
                    appInfo.setCategoryLevel3Name(appCategoryMapper.qryCategoryById(appInfo.getCategoryLevel3()).getCategoryName());
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
            LOOGER.error("AppInfoServiceImpl.getAllAppInfoByMsg 查询异常》》》"+e.getMessage());
        }
        return appInfoList;
    }

    @Override
    public boolean modify(AppInfo appInfo) throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        if(appInfoMapper.modify(appInfo) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public AppInfo getAppInfo(Integer id, String APKName) throws Exception {
        return appInfoMapper.getAppInfo(id,APKName);
    }

    @Override
    public boolean deleteAppLogo(Integer id) throws Exception {
        boolean flag = false;
        if(appInfoMapper.deleteAppLogo(id) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public AppInfo getAppInfo(Integer id) throws Exception {
        return appInfoMapper.getAppInfo(id);
    }

    @Override
    public boolean deleteAppInfoById(Integer delId) throws Exception {
        boolean flag = false;
        if(appInfoMapper.deleteAppInfoById(delId) > 0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean appsysUpdateSaleStatusByAppId(AppInfo appInfoObj) throws Exception {
       /*
		 * 上架：
			1 更改status由【2 or 5】 to 4 ， 上架时间
			2 根据versionid 更新 publishStauts 为 2

			下架：
			更改status 由4给为5
		 */

//        Integer operator = appInfoObj.getModifyBy();
//        if(operator < 0 || appInfoObj.getId() < 0 ){
//            throw new Exception();
//        }

        //get appinfo by appid
        AppInfo appInfo = appInfoMapper.getAppInfo(appInfoObj.getId());
        if(null == appInfo){
            return false;
        }else{
            switch (appInfo.getStatus()) {
                case 2: //当状态为审核通过时，可以进行上架操作
                    onSale(appInfo,4);
                    break;
                case 5://当状态为下架时，可以进行上架操作
                    onSale(appInfo,4);
                    break;
                case 4://当状态为上架时，可以进行下架操作
                    offSale(appInfo,5);
                    break;

                default:
                    return false;
            }
        }
        return true;
    }

    private void onSale(AppInfo appInfo,Integer appInfStatus) throws Exception{
        offSale(appInfo,appInfStatus);
       // setSaleSwitchToAppVersion(appInfo,versionStatus);
    }

    private boolean offSale(AppInfo appInfo,Integer appInfStatus) throws Exception{
        AppInfo _appInfo = new AppInfo();
        //_appInfo.setId(appInfo.getId());
        _appInfo.setStatus(appInfStatus);
       // _appInfo.setModifyBy(operator);
        _appInfo.setOffSaleDate(new Date(System.currentTimeMillis()));
        appInfoMapper.modify(_appInfo);
        return true;
    }

//    private boolean setSaleSwitchToAppVersion(AppInfo appInfo,Integer saleStatus) throws Exception{
//        AppVersion appVersion = new AppVersion();
//        appVersion.setId(appInfo.getVersionId());
//        appVersion.setPublishStatus(saleStatus);
//        //appVersion.setModifyBy(operator);
//        appVersion.setModifyDate(new Date(System.currentTimeMillis()));
//        appVersionMapper.modify(appVersion);
//        return false;
//    }
}
